﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Rose.Models;
using Rose.Helper;


namespace Rose.Controllers
{
    public class SearchController : Controller
    {


        private Database_RoseEntities db = new Database_RoseEntities();
        [Authorize]
        public ActionResult MoreRecommend()
        {
            RecommendHelpModel recommendhelpmodel = new RecommendHelpModel();
            const int PAGESIZE = 10;
            int currentpage = 1;
            bool isOppGender = false;
            bool isDescending = true;
            string type = "university";

            string selfId = new CheckHelpModel().CheckCurrentUserId();

            int allCount = 0;
            List<RecommendModel> recommendList = new List<RecommendModel>();
            string lowertype = type.ToLower();
            switch (lowertype)
            {
                case "samemajor":

                    allCount = recommendhelpmodel.GetMajorFriendsCount(selfId, isOppGender);
                    recommendList = recommendhelpmodel.GetMajorRecommendFriend(selfId, isOppGender, isDescending, currentpage, PAGESIZE);
                    break;
                case "university":
                    allCount = recommendhelpmodel.GetUniversityFriendsCount(selfId, isOppGender);
                    recommendList = recommendhelpmodel.GetUniversityRecommendFriend(selfId, isOppGender, isDescending, currentpage, PAGESIZE);
                    break;
                case "address":
                    allCount = recommendhelpmodel.GetAddressFriendsCount(selfId, isOppGender);
                    recommendList = recommendhelpmodel.GetAddressRecommendFriend(selfId, isOppGender, isDescending, currentpage, PAGESIZE);
                    break;
                default:
                    allCount = recommendhelpmodel.GetMajorFriendsCount(selfId, lowertype, isOppGender);
                    recommendList = recommendhelpmodel.GetMajorRecommendFriend(selfId, lowertype, isOppGender, isDescending, currentpage, PAGESIZE);
                    break;
            }


           
                return View(recommendList);
            
        }


        [Authorize]
        public ActionResult SearchFriendList(string name,bool isSameSchool,int currentpage=1)
        {
            const int PAGESIZE = 10;

            SearchHelpModel help=new SearchHelpModel();

            string selfId = new CheckHelpModel().CheckCurrentUserId();
            string university = new  CheckHelpModel().CheckUserInfo(selfId).University;
            int allcount=help.GetSearchFriendsCount(name, university, isSameSchool);
            ViewData["AllCount"] = allcount;

            ViewData["MaxPage"] = allcount % PAGESIZE == 0 ? allcount / PAGESIZE : allcount / PAGESIZE + 1;

            return View(help.GetSearchFriends(name, university, isSameSchool,currentpage,PAGESIZE));

        }

        [Authorize]
        public ActionResult SearchByCollege(int collegeId,int currentpage=1)
        {
            string college = new CheckHelpModel().CheckCollege(collegeId);
            const int PAGESIZE = 10;

            SearchHelpModel help = new SearchHelpModel();

            string selfId = new CheckHelpModel().CheckCurrentUserId();
            string university = new CheckHelpModel().CheckUserInfo(selfId).University;

            int allcount = help.GetCollegeFriendsCount(college, university);
            ViewData["AllCount"] = allcount;
            ViewData["CollegeName"] = college;

            ViewData["MaxPage"] = allcount % PAGESIZE == 0 ? allcount / PAGESIZE : allcount / PAGESIZE + 1;

            

            return View(help.GetCollegeFriends(college, university, currentpage, PAGESIZE));
        }

        [Authorize]
        public ActionResult SearchByCollegeName(string college)
        {
            const int PAGESIZE=10;
            string selfId=new CheckHelpModel().CheckCurrentUserId();
           
           
            if (string.IsNullOrEmpty(college))
                return Content("院系名不能为空");
            SearchHelpModel help = new SearchHelpModel();
            string university = new CheckHelpModel().CheckUserInfo(selfId).University;

            int allcount = help.GetCollegeFriendsCount(college, university);
            ViewData["CollegeName"] = college;
            ViewData["AllCount"] = allcount;
            ViewData["MaxPage"] = allcount % PAGESIZE == 0 ? allcount / PAGESIZE : allcount / PAGESIZE + 1;
            return View(help.GetCollegeFriends(college, university));


        }
        






        [Authorize]
        public ActionResult SearchByCollegePart()
        {
            CheckHelpModel help = new CheckHelpModel();
            string selfId = help.CheckCurrentUserId();
            UserInfo selfInfo = new CheckHelpModel().CheckUserInfo(selfId);
            ListHelpModel listhelp = new ListHelpModel();
           var university= db.Universities.Where(c => c.Name == selfInfo.University).FirstOrDefault();
           if (university == null)
               return Content("错误");
           int schoolId = university.UniversityId;
           ViewData["AllCollege"] = listhelp.GetCollegeSelectList(schoolId);
           return View();
        }


        public ActionResult SearchBarPart()
        {
            return View();
        }

    }

    
}
